package com.mxsoft.ssmdemo.web.controller;

import com.mxsoft.ssmdemo.model.Foo;
import com.mxsoft.ssmdemo.service.IFooService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhangyingxuan
 */
@RestController
@RequestMapping("/api/foo")
public class FooController {

    @Autowired
    private IFooService fooService;

    @RequestMapping( method = RequestMethod.POST)
    public Object add(@RequestBody Foo foo) {
        int add = fooService.add(foo);
        if (add > 0) {
            return foo;
        }
        return null;
    }

    @RequestMapping(method = RequestMethod.PUT)
    public Object edit(Foo foo) {
        int add = fooService.edit(foo);
        if (add > 0) {
            return foo;
        }
        return null;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Object delete(@PathVariable("id") Long id) {
        int add = fooService.delete(id);
        if (add > 0) {
            return add;
        }
        return null;
    }

    @RequestMapping(method = RequestMethod.GET)
    public Object findByID() {
        List<Foo> fooList = fooService.findAll();
        return fooList;
    }

}
